home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / misc / emu / Apex-src.lha / BUG.68K < prev    next >
Text File  |  2001-09-30  |  44KB  |  1,504 lines

  1. ;BUG.68K    APR-21-88
  2. ;A debugger similar to Stride's SDT.
  3. ; by Loren Blaney
  4. ;
  5. ;REVISION HISTORY:
  6. ;FEB-22-87, Original based on version by King & Knight, transcribed by
  7. ;    John Klein
  8. ;APR-21-88, Modified for Mac II. Uses Apex system console.
  9. ;
  10. ;COMMANDS:
  11. ;
  12. ; DISPLAY:
  13. ;  Dm addr    Display memory
  14. ;  DR        Display registers
  15. ;  DDn        Display Data registers (n is ignored)
  16. ;  DAn        Display Address registers
  17. ;  DPc        Display Program Counter register
  18. ;  DUsp        Display User Stack Pointer register (SSP = A7)
  19. ;  DSr        Display Status register (not SSP)
  20. ;  DBn        Display breakpoints
  21. ;
  22. ; SET:
  23. ;  Sm addr    Set memory
  24. ;  SR val    Set register (begins with D0)
  25. ;  SDn val    Set Data register
  26. ;  SAn val    Set Address register
  27. ;  SPc val    Set Program Counter register
  28. ;  SUsp val    Set User Stack Pointer register
  29. ;  SSr val    Set Status register
  30. ;  SBn val    Set breakpoint (0= cleared)
  31. ;
  32. ;    SET MODE COMMANDS:
  33. ;        00000400: 2345 = val
  34. ;        00000402: 0AF0 =
  35. ;        Return and down arrow move to next word
  36. ;        Up arrow moves back a word
  37. ;        Multiple entries may be made on one line
  38. ;        Period (.) (RETURN) terminates entry mode
  39. ;        Registers are set the same way
  40. ;
  41. ;
  42. ; EXECUTE:
  43. ;  Go pc    Go execute at address (PC). Defaults to current PC
  44. ;        if address is not specified. That is, it resumes after
  45. ;        a breakpoint. Notice that if you Go directly to a
  46. ;        location which contains a breakpoint, it will not break.
  47. ;  Tr pc    Trace beginning at address (PC). Defaults work same as
  48. ;        Go command. Breakpoints have no effect.
  49. ;
  50. ;
  51. ; MISCELLENA
  52. ;  F s e p    Fill (byte, word, or long is determined by pattern size)
  53. ;  M s e t    Move memory start.end>to
  54. ;* C s e t    Compare memory start.end>to
  55. ;* L s e p    Look for pattern (mask byte??)
  56. ;* H        Help (also help key)
  57. ;* R u b a s    Read and write disk blocks (unit, block, addr, size)
  58. ;* W u b a s
  59. ;
  60. ;  Z bytes    Set frame size for memory dump
  61. ;*        Download
  62. ;  CTRL-C    Jump back to main command mode
  63. ;  CTRL-P    Restart and reinitialize the debugger
  64. ;
  65. ;* Indicates unimplemented features.
  66. ;
  67. ;======================================================================
  68.     NOLIST
  69.     INCLUDE    SYSPAG
  70.     LIST
  71.  
  72. ;ADDITIONAL ASCII CHARACTERS:
  73. UP    EQU    'K'-CTRL    ;Up arrow
  74. DOWN    EQU    'J'-CTRL    ;Down arrow
  75.  
  76. SSPDEF    EQU    $3000        ;Default supervisor stack pointer
  77.  
  78. SRDEF    EQU    $2000        ;Supervisor mode
  79.  
  80. SIZEDEF    EQU    $40        ;Default memory display frame size
  81.  
  82. ;----------------------------------------------------------------------
  83.     ORG    $400
  84.     JMP    START.L
  85.  
  86. RAMBASE    ORG    $1000
  87.  
  88. ;The following tables must be in order because of the way SETREG works
  89. SAVD    DS.L    8        ;Space for 8 data registers
  90. SAVA    DS.L    8        ;Space for 8 address registers
  91. SAVPC    DS.L    1        ;Space for program counter
  92. SAVUSP    DS.L    1        ;Space for user stack pointer
  93. SAVSR    DS.L    1        ;Space for status register
  94.     DS.L    1        ;Dummy spacer
  95. BRKITBL    DS.W    8        ;Space for 8 breakpoint instructions
  96. BRKATBL    DS.L    8        ;Space for 8 breakpoint addresses
  97.                 ; (only the low word is actually used)
  98. ; (end order restriction)
  99.  
  100. ACCESS    DS.L    1        ;Bus and address error access address
  101. FUNCODE    DS.W    1        ;Bus and address error function code
  102.  
  103. ADDRDIS    DS.L    1        ;Default address for memory display
  104. ADDRSET    DS.L    1        ;Default address for memory set
  105. SIZEDIS    DS.L    1        ;Memory display frame size (bytes)
  106.  
  107. BPFLAG    DS.B    1        ;Flag: resume execution thru breakpoint
  108. GOFLAG    DS.B    1        ;Flag: user's program is running
  109.  
  110. BKCHAR    DS.B    1        ;Last char read by CHIN
  111. BKFLAG    DS.B    1        ;Flag: BACKUP command, reread last char
  112.  
  113. RAMEND    EQU    @        ;End of RAM area (+1)
  114.  
  115. ;======================================================================
  116. ;DEBUGGER ENTRY POINT
  117. ;
  118. BASE    EQU    $3000
  119.     ORG    BASE
  120. ;    ORG    $7EF00
  121. START    BSR    INIT
  122.  
  123. RESTART    BSR    CRLF        ;(Entry from CTRL-C)
  124.  
  125. GETCMD    BSR    TEXT        ;Display prompt message
  126.     ASCII    'BUG'
  127.     DC.B    '>' + $80
  128.  
  129.     BSR    OPENI        ;Flush keyboard buffer
  130.     BSR    GETCH        ;Get uppercase character into D0
  131.     LEA    CMDTBL.L,A0    ;Point to command search table
  132.     BSR    EXECUTE        ;Call routine to handle command
  133.     BRA.S    GETCMD        ;Loop back
  134.  
  135. ;Main command table:
  136. CMDTBL    DC.W    'D'        ;Display memory and registers
  137.     DC.W    CMD_D_-CMDTBL
  138.     DC.W    CR        ;Display memory
  139.     DC.W    CMD_DM-CMDTBL
  140.     DC.W    DOWN        ;Display memory
  141.     DC.W    CMD_DM-CMDTBL
  142.     DC.W    UP        ;Display memory
  143.     DC.W    CMD_DMU-CMDTBL
  144.     DC.W    'S'        ;Set memory and registers
  145.     DC.W    CMD_S_-CMDTBL
  146.     DC.W    'G'        ;Start or continue program (Go)
  147.     DC.W    CMD_G-CMDTBL
  148.     DC.W    'T'        ;Trace program
  149.     DC.W    CMD_T-CMDTBL
  150.     DC.W    'F'        ;Fill memory with pattern
  151.     DC.W    CMD_F-CMDTBL
  152.     DC.W    'M'        ;Move a block of memory
  153.     DC.W    CMD_M-CMDTBL
  154.     DC.W    'Z'        ;Move a block of memory
  155.     DC.W    CMD_Z-CMDTBL
  156.     DC.W    $00        ;Mark end of table
  157.     DC.W    CMD_BAD-CMDTBL    ;Routine to handle illegal commands
  158.  
  159. ;----------------------------------------------------------------------
  160. ;Routine to handle illegal commands
  161. ;
  162. CMD_BAD    BSR    TEXT
  163.     ASCII    'I BEG YOUR PARDON?'
  164.     DC.B    BEL, CR + $80
  165.     RTS
  166.  
  167. ;----------------------------------------------------------------------
  168. ;Initialization stuff (mostly cold start)
  169. ;
  170. INIT    BSR    OPEN
  171.  
  172. ;Set up the exception vectors.
  173. ;First, set all vectors to the unassigned exception handler.
  174. ;This fills memory locations $0000 - $00FF.
  175.     LEA    UA_EXCP.L,A1    ;Address of unassigned exception handler
  176.     SUBA.L    A0,A0        ;Point to location 0
  177.     MOVE.W    #63,D0        ;Get number of vectors (-1)
  178. INIT10    MOVE.L    A1,(A0)+
  179.     DBF    D0,INIT10
  180.  
  181.     LEA    IN_EXCP.L,A1    ;Address of unassigned interrupt handler
  182.     MOVE.W    #191,D0        ;Get number of vectors (-1)
  183. INIT15    MOVE.L    A1,(A0)+    ;Resume loading where unassigned vectors
  184.     DBF    D0,INIT15    ; left off
  185.  
  186.     LEA    $64,A0        ;Point to start of interrupt autovectors
  187.     MOVE.W    #7,D0        ;Get number of vectors (-1)
  188. INIT17    MOVE.L    A1,(A0)+
  189.     DBF    D0,INIT17
  190.  
  191.     LEA    TI_EXCP.L,A1
  192.     LEA    $80,A0        ;Set up TRAP vectors
  193.     MOVE.W    #15,D0        ;Get number of exception vectors (-1)
  194. INIT19    MOVE.L    A1,(A0)+
  195.     DBF    D0,INIT19
  196.  
  197. ;Now, set up the other vectors.
  198.     LEA    VECTBL.L,A1    ;Point to table of vectors
  199.     SUBA.L    A0,A0        ;Point to location 0
  200.     MOVE.W    #11,D0        ;Get number of exception vectors -1
  201. INIT20    MOVE.L    (A1)+,(A0)+
  202.     DBF    D0,INIT20
  203.  
  204.     LEA    $60,A0        ;Set up spurious interrupt vector
  205.     MOVE.L    (A1)+,(A0)
  206.     LEA    $BC,A0        ;Set up breakpoint TRAP (15)
  207.     MOVE.L    (A1)+,(A0)
  208.  
  209.     LEA    RAMBASE.L,A0    ;Clear all variables
  210.     MOVE.W    #[RAMEND-RAMBASE]/4-1,D0 ;Size of area to clear (-1)
  211. INIT30    CLR.L    (A0)+        ;Clear data area
  212.     DBF    D0,INIT30
  213.  
  214.     MOVE.L    #SSPDEF,SAVA+28    ;Set default supervisor statck pointer
  215.     MOVE.W    #SRDEF,SAVSR+2    ;Set default status register
  216.     MOVE.L    #SIZEDEF,SIZEDIS ;Set default memory display size
  217.  
  218.     BSR    TEXT        ;Display sign-on message
  219.     DC.B    CR
  220.     ASCII    '-- DEBUGGER, V1.0x1 --'
  221.     DC.B    CR + $80
  222.     RTS
  223.  
  224. ;Vector table
  225. VECTBL    DC.L    SSPDEF        ;0 Reset:  Initial SSP
  226.     DC.L    START        ;1 Reset: Initial PC
  227.     DC.L    BE_EXCP        ;2 Bus Error
  228.     DC.L    AE_EXCP        ;3 Address Error
  229.     DC.L    II_EXCP        ;4 Illegal Instruction
  230.     DC.L    ZD_EXCP        ;5 Zero Divide
  231.     DC.L    CI_EXCP        ;6 CHK Instruction
  232.     DC.L    TV_EXCP        ;7 TRAPV Instruction
  233.     DC.L    PV_EXCP        ;8 Privilege Violation
  234.     DC.L    TR_EXCP        ;9 Trace
  235.     DC.L    LA_EXCP        ;10 Line 1010 Emulator
  236.     DC.L    LF_EXCP        ;11 Line 1111 Emulator
  237.  
  238.     DC.L    SI_EXCP        ;12 Spurious Interrupt
  239.     DC.L    BP_EXCP        ;15 Breakpoint Trap
  240.  
  241. ;======================================================================
  242. ;DISPLAY COMMANDS
  243. ;----------------------------------------------------------------------
  244. ;Dispatch to display routine
  245. ;
  246. CMD_D_    BSR    GETCH        ;Get uppercase character into D0
  247.     LEA    DCMDTBL.L,A0    ;Point to command search table
  248.     BRA    EXECUTE        ;(PBRA) Call routine to handle command
  249.  
  250. ;Display command table:
  251. DCMDTBL    DC.W    'M'        ;Display memory
  252.     DC.W    CMD_DM-DCMDTBL
  253.     DC.W    CR        ;Display memory
  254.     DC.W    CMD_DM-DCMDTBL
  255.     DC.W    SPACE        ;Display memory    ???
  256.     DC.W    CMD_DM-DCMDTBL
  257.     DC.W    'R'        ;Display all registers
  258.     DC.W    CMD_DR-DCMDTBL
  259.     DC.W    'D'        ;Display data registers
  260.     DC.W    CMD_DD-DCMDTBL
  261.     DC.W    'A'        ;Display address registers
  262.     DC.W    CMD_DA-DCMDTBL
  263.     DC.W    'P'        ;Display program counter
  264.     DC.W    CMD_DP-DCMDTBL
  265.     DC.W    'U'        ;Display user stack pointer
  266.     DC.W    CMD_DU-DCMDTBL
  267.     DC.W    'S'        ;Display status register
  268.     DC.W    CMD_DS-DCMDTBL
  269.     DC.W    'B'        ;Display breakpoints
  270.     DC.W    CMD_DB-DCMDTBL
  271.     DC.W    $00        ;Mark end of table
  272.     DC.W    CMD_BAD-DCMDTBL    ;Routine to handle illegal commands
  273.  
  274. ;----------------------------------------------------------------------
  275. ;Up arrow command -- back up and display memory
  276. ;
  277. CMD_DMU    MOVE.L    SIZEDIS,D0    ;Back up two frame lengths
  278.     ADD.L    D0,D0
  279.     SUB.L    D0,ADDRDIS    ;(PFALL) Fall into display memory
  280.  
  281. ;----------------------------------------------------------------------
  282. ;DM command -- display memory
  283. ; Format:
  284. ;00000400:  0000 1111 2222 3333   4444 5555 6666 7777   ........  ......
  285. ;
  286. ; Destroys registers D0, A0, and A1
  287. ;
  288. CMD_DM    BSR    BACKUP
  289.     BSR    HEXIN        ;Get address
  290.     BEQ.S    CDM01        ;Branch if no address given
  291.     MOVE.L    D0,ADDRDIS    ;Set newly specified address
  292.  
  293. CDM01    MOVEA.L    ADDRDIS,A0    ;Get address for start of display
  294.  
  295. CDM00    MOVE.L    A0,D0        ;Display the address
  296.     BSR    HEX8OUT
  297.     BSR    TEXT        ;Follow it with a colon and 2 spaces
  298.     ASCII    ': '
  299.     DC.B    SPACE + $80
  300.  
  301.     MOVEA.L    A0,A1        ;Get working copy of address into A1
  302. CDM10    MOVE.B    (A1)+,D0    ;Display the byte at the address
  303.     BSR    HEX2OUT
  304.  
  305.     MOVE.W    A1,D0        ;Get current address
  306.     BTST    #0,D0        ;Is it odd or even?
  307.     BNE.S    CDM10        ;Branch back if it is odd
  308.     BSR    SPOUT        ;Display a space between words
  309.  
  310.     ANDI.B    #$07,D0        ;Is current address evenly divisible by
  311.     BNE.S    CDM10        ; 8?  Branch if not
  312.     BSR    SPOUT        ;Display 2 more spaces
  313.     BSR    SPOUT
  314.  
  315.     MOVE.W    A1,D0        ;Get current address
  316.     ANDI.B    #$0F,D0        ;Is it evenly divisible by 16?
  317.     BNE.S    CDM10        ;Branch if not
  318.  
  319. CDM30    MOVE.B    (A0)+,D0    ;Display the ASCII values...
  320.     ANDI.B    #$7F,D0        ;Convert byte to ASCII
  321.     CMPI.B    #$20,D0        ;Is it a displayable character?
  322.     BHS.S    CDM40        ;Branch if yes
  323.     MOVEQ    #'.',D0        ;Else replace it with a dot
  324. CDM40
  325.     CMPI.B    #$7F,D0        ;Is it a delete character?
  326.     BNE.S    CDM50        ;Branch if not
  327.     MOVEQ    #'.',D0        ;Else replace it with a dot
  328. CDM50
  329.     BSR    CHOUT        ;Display ASCII character
  330.  
  331.     MOVE.W    A0,D0        ;Put 2 spaces after the 8th byte
  332.     ANDI.B    #$07,D0
  333.     BNE.S    CDM30
  334.     BSR    SPOUT
  335.     BSR    SPOUT
  336.  
  337.     MOVE.W    A0,D0        ;Put a CRLF after the 16th byte
  338.     ANDI.B    #$0F,D0
  339.     BNE.S    CDM30
  340.     BSR    CRLF
  341.  
  342.     MOVEA.L    ADDRDIS,A1    ;Is A1 >= Frame length + ADDRDIS?
  343.     ADDA.L    SIZEDIS,A1
  344.     CMPA.L    A1,A0
  345.     BLO.S    CDM00        ;Loop back if not
  346.     MOVE.L    A0,ADDRDIS    ;Else update default address and exit
  347.     RTS
  348.  
  349. ;----------------------------------------------------------------------
  350. ;DR command -- display all registers
  351. ;
  352. CMD_DR    BSR.S    CMD_DD        ;Display data registers
  353.     BSR.S    CMD_DA        ;Display address registers
  354.     BSR.S    CMD_DP        ;Display PC, etc.
  355.     BRA.S    CMD_DB        ;(PBRA) Display breakpoints
  356.  
  357. ;----------------------------------------------------------------------
  358. ;DD command -- display data registers
  359. ;
  360. CMD_DD    LEA    SAVD.L,A0        ;Point to data register table
  361.     MOVEQ    #'D',D0        ;Get register letter
  362.     BRA.S    CDR_        ;(PBRA) Display registers
  363.  
  364. ;----------------------------------------------------------------------
  365. ;DA command -- display address registers
  366. ;
  367. CMD_DA    LEA    SAVA.L,A0        ;Point to address register table
  368.     MOVEQ    #'A',D0        ;Get register letter
  369.     BRA.S    CDR_        ;(PBRA) Display registers
  370.  
  371. ;----------------------------------------------------------------------
  372. ;DB command -- display breakpoints
  373. ;
  374. CMD_DB    LEA    BRKATBL.L,A0    ;Point to breakpoint addresses
  375.     MOVEQ    #'B',D0        ;Get register letter
  376.                 ;(PFALL) CDR_ Display registers
  377.  
  378. ;----------------------------------------------------------------------
  379. ;Local subroutine to display registers
  380. ; Inputs:
  381. ;    D0 = Contains character describing register type (D, A, or B)
  382. ;    A0 = Pointer to table of values for each register
  383. ; Destroys D0, D1 and A0.
  384. ; Format:
  385. ; D0:  01234567 01234567 01234567 01234567   01234567 01234567 ...
  386. ;
  387. CDR_    BSR    CHOUT        ;Display D, A, or B
  388.  
  389.     BSR    TEXT
  390.     ASCII    '0: '
  391.     DC.B    SPACE + $80
  392.  
  393.     MOVEQ    #7,D1
  394. CDR10    MOVE.L    (A0)+,D0    ;Get the register's value
  395.     BSR    HEX8OUT        ;Display it
  396.     BSR    SPOUT        ;Followed by a space
  397.  
  398.     CMP.B    #4,D1        ;Display 2 more spaces before register 4
  399.     BNE.S    CDR20        ;Branch if not register 4
  400.     BSR    SPOUT
  401.     BSR    SPOUT
  402.  
  403. CDR20    DBF    D1,CDR10
  404.  
  405.     BRA    CRLF        ;(PBRA) Move to next line and return
  406.  
  407. ;----------------------------------------------------------------------
  408. ;DPc, DUsp, & DSr commands -- display program counter, user stack
  409. ; pointer, and status register.
  410. ; Registers D0-D2/A0 are destroyed.
  411. ;
  412. CMD_DP
  413. CMD_DU
  414. CMD_DS    BSR    TEXT        ;Display program counter
  415.     ASCII    'PC: '
  416.     DC.B    SPACE + $80
  417.     MOVE.L    SAVPC,D0
  418.     BSR    HEX8OUT
  419.  
  420.     BSR    TEXT        ;Display user stack pointer
  421.     ASCII    '   USP: '
  422.     DC.B    SPACE + $80
  423.     MOVE.L    SAVUSP,D0
  424.     BSR    HEX8OUT
  425.  
  426.     BSR    TEXT        ;Display status register
  427.     ASCII    '   SR: '
  428.     DC.B    SPACE + $80
  429.     MOVE.L    SAVSR,D0
  430.     BSR    HEX4OUT
  431.  
  432.     BSR    TEXT        ;Show status register bits
  433.     ASCII    ' '
  434.     DC.B    '(' + $80
  435.  
  436.     MOVE.W    D0,D1
  437.     LEA    FLAGS.L,A0    ;Point to list of flag names
  438.     MOVEQ    #15,D2        ;Display T if trace flag is set
  439.     BSR.S    FLGOUT
  440.     MOVEQ    #13,D2        ;Display S if supervisor state
  441.     BSR.S    FLGOUT
  442.  
  443.     MOVE.W    D1,D0        ;Display interrupt level (0..7)
  444.     LSR.W    #8,D0
  445.     BSR    HEX1OUT
  446.  
  447.     MOVEQ    #4,D2        ;Display status flags: XNZVC
  448. CDS20    BSR.S    FLGOUT
  449.     DBF    D2,CDS20
  450.  
  451.     BSR    TEXT
  452.     DC.B    ')', CR + $80
  453.     RTS
  454.  
  455. ;----------------------------------------------------------------------
  456. ;Local subroutine to display flag bits.
  457. ; Inputs:
  458. ;    D1 = Copy of status register
  459. ;    D2 = Bit (flag) in status register to display
  460. ;    A0 = Pointer to string of flag names
  461. ; D0 is destroyed.
  462. ;
  463. FLGOUT    MOVE.B    (A0)+,D0    ;Assume flag is set
  464.     BTST    D2,D1        ;Test bit in copy of status register
  465.     BNE.S    FLO10        ;Branch if flag is in fact set
  466.     MOVEQ    #SPACE,D0    ;Else substitute a space character
  467. FLO10    BRA    CHOUT        ;(PBRA) Output character and return
  468.  
  469. FLAGS    ASCII    'TSXNZVC'    ;Status register flag names
  470.  
  471. ;======================================================================
  472. ;SET COMMANDS
  473. ;----------------------------------------------------------------------
  474. ;Dispatch to routine to set memory and registers
  475. CMD_S_    BSR    GETCH        ;Get uppercase character into D0
  476.     LEA    SCMDTBL.L,A0    ;Point to command search table
  477.     BRA    EXECUTE        ;(PBRA) Call routine to handle command
  478.  
  479. ;Set command table:
  480. SCMDTBL    DC.W    'M'        ;Set memory
  481.     DC.W    CMD_SM-SCMDTBL
  482.     DC.W    CR        ;Set memory
  483.     DC.W    CMD_SM-SCMDTBL
  484.     DC.W    SPACE        ;Set memory
  485.     DC.W    CMD_SM-SCMDTBL
  486.     DC.W    'R'        ;Set all registers
  487.     DC.W    CMD_SR-SCMDTBL
  488.     DC.W    'D'        ;Set data registers
  489.     DC.W    CMD_SD-SCMDTBL
  490.     DC.W    'A'        ;Set address registers
  491.     DC.W    CMD_SA-SCMDTBL
  492.     DC.W    'P'        ;Set program counter
  493.     DC.W    CMD_SP-SCMDTBL
  494.     DC.W    'U'        ;Set user stack pointer
  495.     DC.W    CMD_SU-SCMDTBL
  496.     DC.W    'S'        ;Set status register
  497.     DC.W    CMD_SS-SCMDTBL
  498.     DC.W    'B'        ;Set breakpoints
  499.     DC.W    CMD_SB-SCMDTBL
  500.     DC.W    $00        ;Mark end of table
  501.     DC.W    CMD_BAD-SCMDTBL    ;Routine to handle illegal commands
  502.  
  503. ;----------------------------------------------------------------------
  504. ;SM command -- set memory
  505. ; Destroys registers D0 and A0
  506. ;
  507. CMD_SM    CMPI.B    #CR,D0        ;If last char input was a CR then
  508.     BEQ.S    CSM10        ; don't try to look for hex (hangs)
  509.  
  510.     BSR    HEXIN        ;Get address
  511.     BEQ.S    CSM10        ;Branch if no address given
  512.     MOVE.L    D0,ADDRSET    ;Set newly specified address
  513.  
  514. CSM10    MOVEA.L    ADDRSET,A0    ;Get address for start of display
  515.  
  516.     MOVE.L    A0,D0        ;Display the address
  517.     BSR    HEX8OUT
  518.     BSR    TEXT        ;Follow it with a colon and 2 spaces
  519.     ASCII    ': '
  520.     DC.B    SPACE + $80
  521.  
  522.     MOVE.B    (A0)+,D0    ;Display the byte at the address
  523.     BSR    HEX2OUT
  524.     MOVE.B    (A0)+,D0    ;Display the next byte
  525.     BSR    HEX2OUT
  526.  
  527.     BSR    TEXT        ;Follow it with a...
  528.     ASCII    ' ='
  529.     DC.B    SPACE + $80
  530.  
  531.     BSR    GETCH        ;Get uppercase character into D0
  532.     LEA    XCMDTBL.L,A0    ;Point to command search table
  533.     BSR    EXECUTE        ;Call routine to handle command
  534.     BRA.S    CSM10        ;Loop until "." (or CTRL-C)
  535.  
  536. ;Set-memory command table:
  537. XCMDTBL    DC.W    CR        ;Move to next location
  538.     DC.W    CSMDOWN-XCMDTBL
  539.     DC.W    DOWN
  540.     DC.W    CSMDOWN-XCMDTBL
  541.     DC.W    UP
  542.     DC.W    CSMUP-XCMDTBL
  543.     DC.W    '.'
  544.     DC.W    CSMEXIT-XCMDTBL
  545.     DC.W    $00        ;End marker flag
  546.     DC.W    CSMASCI-XCMDTBL    ;Assume ASCII and change memory contents
  547.  
  548. ;----------------------------------------------------------------------
  549. ;Return from CMD_SM (and SETREGS).
  550. ;
  551. CSMEXIT    ADDQ.L    #4,SP        ;Discard first return address
  552.     RTS
  553.  
  554. ;----------------------------------------------------------------------
  555. ;Move ahead (or down) one word in memory.
  556. ;
  557. CSMDOWN    ADDQ.L    #2,ADDRSET
  558.     RTS
  559.  
  560. ;----------------------------------------------------------------------
  561. ;Move back (or up) one word in memory.
  562. ;
  563. CSMUP    SUBQ.L    #2,ADDRSET
  564.     RTS
  565.  
  566. ;----------------------------------------------------------------------
  567. ;Get new value to store into current word
  568. ;
  569. CSMASCI    MOVEM.L    D0/A0,-(SP)    ;Save registers
  570.  
  571.     BSR    BACKUP        ;Reread first hex digit
  572.     BSR    HEXIN
  573.     BNE.S    CSMA10        ;Branch if no error
  574.  
  575.     BSR    TEXT
  576.     ASCII    'HEX NUMBER EXPECTED'
  577.     DC.B    BEL, CR + $80
  578.     BSR    OPENI        ;Flush the line buffer
  579.     BRA.S    CSMA90        ;Exit
  580. CSMA10
  581.     MOVEA.L    ADDRSET,A0    ;Get current address
  582.     MOVE.W    D0,(A0)+    ;Store hex into word
  583.     MOVE.L    A0,ADDRSET
  584.  
  585. CSMA90    MOVEM.L    (SP)+,D0/A0    ;Restore registers
  586.     RTS
  587.  
  588. ;----------------------------------------------------------------------
  589. ;SD command -- set data registers
  590. ; Destroys registers D0 and D1
  591. ;
  592. CMD_SR                ;Also, SR command -- set all registers
  593. CMD_SD    BSR    HEXIN        ;Get register number
  594.     ANDI.B    #$07,D0        ;Force to legal range
  595.     BRA.S    SETREGS        ;Enter set-register code
  596.  
  597. ;----------------------------------------------------------------------
  598. ;SA command -- set address registers
  599. ;
  600. CMD_SA    BSR    HEXIN        ;Get register number
  601.     ANDI.B    #$07,D0        ;Force to legal range
  602.     ADDQ.L    #8,D0        ;Add address register table offset
  603.     BRA.S    SETREGS        ;Enter set-register code
  604.  
  605. ;----------------------------------------------------------------------
  606. ;SP command -- set value of program counter
  607. ;
  608. CMD_SP    MOVEQ    #$10,D0        ;Get offset to PC table entry
  609.     BRA.S    SETREGS        ;Enter set-register code
  610.  
  611. ;----------------------------------------------------------------------
  612. ;SU command -- set user stack pointer
  613. ;
  614. CMD_SU    MOVEQ    #$11,D0        ;Get offset to US table entry
  615.     BRA.S    SETREGS        ;Enter set-register code
  616.  
  617. ;----------------------------------------------------------------------
  618. ;SS command -- set value of status register
  619. ;
  620. CMD_SS    MOVEQ    #$12,D0        ;Get offset to SR table entry
  621.     BRA.S    SETREGS        ;Enter set-register code
  622.  
  623. ;----------------------------------------------------------------------
  624. ;SB command -- set breakpoints
  625. ;
  626. CMD_SB    BSR    HEXIN        ;Get breakpoint number
  627.     ANDI.B    #$07,D0        ;Force to legal range
  628.     ADDI.B    #$18,D0        ;Add breakpoint table offset
  629.                 ;Enter set-register code
  630.  
  631. ;----------------------------------------------------------------------
  632. ;Set register values (D0-D7, A0-A7, PC, USP, SR, B0-B7).
  633. ; Inputs:
  634. ;    D0 = Register number (0..$12)
  635. ;
  636. ; Destroys registers D0, D1, and A1
  637. ;
  638. SETREGS    BSR    OPENI        ;Flush line to save confusion
  639.     MOVE.L    D0,D1        ;Save the register number in D1
  640.  
  641. SETR00    MOVE.L    D1,D0        ;A1 = D1 * 4 + SAVD
  642.     ASL.L    #2,D0
  643.     ADD.L    #SAVD,D0
  644.     MOVEA.L    D0,A1
  645.  
  646.     CMPI.B    #$08,D1        ;Is it a data register?
  647.     BGE.S    SETR10        ;Branch if not
  648.     MOVEQ    #'D',D0
  649.     BRA.S    SHOREG
  650. SETR10
  651.     CMPI.B    #$10,D1
  652.     BGE.S    SETR20
  653.     MOVEQ    #'A',D0
  654.     BRA.S    SHOREG
  655. SETR20
  656.     BNE.S    SETR23        ;Branch if not PC
  657.     BSR    TEXT        ;Display program counter
  658.     ASCII    'PC: '
  659.     DC.B    SPACE + $80
  660.     MOVE.L    (A1),D0        ;Display the current value
  661.     BSR    HEX8OUT
  662.     BRA.S    SETR30
  663. SETR23
  664.     CMPI.B    #$11,D1        ;Is it the USP?
  665.     BNE.S    SETR26        ;Branch if not
  666.     BSR    TEXT        ;Display user stack pointer
  667.     ASCII    'USP: '
  668.     DC.B    SPACE + $80
  669.     MOVE.L    (A1),D0        ;Display the current value
  670.     BSR    HEX8OUT
  671.     BRA.S    SETR30
  672. SETR26
  673.     CMPI.B    #$12,D1        ;Is it the SR?
  674.     BNE.S    SETR28        ;Branch if not
  675.     BSR    TEXT        ;Display status register
  676.     ASCII    'SR: '
  677.     DC.B    SPACE + $80
  678.     MOVE.L    (A1),D0        ;Display the current value
  679.     BSR    HEX4OUT
  680.     BRA.S    SETR30
  681. SETR28
  682.     MOVEQ    #'B',D0        ;It must be a breakpoint
  683.  
  684. SHOREG    BSR    CHOUT        ;Display register letter
  685.  
  686.     MOVE.L    D1,D0        ;Get register index
  687.     ANDI.B    #$07,D0        ;Mask to 0..7 range
  688.     BSR    HEX1OUT
  689.  
  690.     BSR    TEXT        ;Follow it with a colon and 2 spaces
  691.     ASCII    ': '
  692.     DC.B    SPACE + $80
  693.  
  694.     MOVE.L    (A1),D0        ;Display the current value
  695.     BSR    HEX8OUT
  696. SETR30
  697.     BSR    TEXT        ;Follow it with a...
  698.     ASCII    ' ='
  699.     DC.B    SPACE + $80
  700.  
  701.     BSR    GETCH        ;Get uppercase character into D0
  702.     LEA    RCMDTBL.L,A0    ;Point to command search table
  703.     BSR    EXECUTE        ;Call routine to handle command
  704.     BRA    SETR00        ;Loop until "." (or CTRL-C)
  705.  
  706. ;Set-register command table:
  707. RCMDTBL    DC.W    '.'
  708.     DC.W    CSMEXIT-RCMDTBL
  709.     DC.W    CR        ;Move to next location
  710.     DC.W    CSRDOWN-RCMDTBL
  711.     DC.W    DOWN
  712.     DC.W    CSRDOWN-RCMDTBL
  713.     DC.W    UP
  714.     DC.W    CSRUP-RCMDTBL
  715.     DC.W    $00        ;End marker flag
  716.     DC.W    CSRASCI-RCMDTBL    ;Assume ASCII and change memory contents
  717.  
  718. ;----------------------------------------------------------------------
  719. ;Move ahead (or down) a register
  720. ;
  721. CSRDOWN    ADDQ.B    #1,D1
  722.     CMPI.B    #$1F,D1
  723.     BLS.S    CSRD10
  724.     MOVEQ    #0,D1
  725. CSRD10    CMPI.B    #$13,D1
  726.     BNE.S    CSRD20
  727.     MOVEQ    #$18,D1
  728. CSRD20    RTS
  729.  
  730. ;----------------------------------------------------------------------
  731. ;Move back (or up) one register
  732. ;
  733. CSRUP    SUBQ.B    #1,D1
  734.     BGE.S    CSRU10
  735.     MOVEQ    #$1F,D1
  736. CSRU10    CMPI.B    #$17,D1
  737.     BNE.S    CSRU20
  738.     MOVEQ    #$12,D1
  739. CSRU20    RTS
  740.  
  741. ;----------------------------------------------------------------------
  742. ;Get new value to store into current register
  743. ; Inputs A1 = Pointer to the entry in the table for the current register
  744. ;
  745. CSRASCI    MOVEM.L    D0,-(SP)    ;Save registers
  746.  
  747.     BSR    BACKUP        ;Reread first hex digit
  748.     BSR    HEXIN
  749.     BNE.S    CSRA10        ;Branch if no error
  750.  
  751.     BSR    TEXT
  752.     ASCII    'HEX NUMBER EXPECTED'
  753.     DC.B    BEL, CR + $80
  754.     BSR    OPENI        ;Flush the line buffer
  755.     BRA.S    CSRA90        ;Exit
  756. CSRA10
  757.     MOVE.L    D0,(A1)        ;Store hex into register save loc
  758.     BSR.S    CSRDOWN        ;Get terminator
  759.  
  760. CSRA90    MOVEM.L    (SP)+,D0    ;Restore registers
  761.     RTS
  762.  
  763. ;======================================================================
  764. ;F command -- fill memory with pattern
  765. ; Three arguments must be given.
  766. ; FILL (START, END, PATTERN)
  767. ; The pattern argument is tested to determine if it is to be repeated
  768. ; every byte, word, or long. Word and long patterns have a couple of
  769. ; problems. Some word and long patterns are impossible, for example
  770. ; $0034 is interpreted as a byte, not a word. FILL (100, 200, 12345678)
  771. ; will fill up through location 203, not 200.
  772. ;
  773. ; Destroys registers D0, D1, and A0.
  774. ;
  775. CMD_F    BSR    HEXIN        ;Get START argument
  776.     BEQ    CMD_BAD        ;(PBRA) Must have 3 hex values
  777.     MOVEA.L    D0,A0        ;Initialize pointer to START
  778.  
  779.     BSR    BACKUP        ;Check for CR
  780.     BSR    HEXIN        ;Get END argument
  781.     BEQ    CMD_BAD        ;(PBRA) Check for 3 hex values
  782.     MOVE.L    D0,D1        ;Save END in D1
  783.     CMP.L    A0,D1        ;If END < START then error
  784.     BLO    CMD_BAD        ;(PBRA)
  785.  
  786.     BSR    BACKUP        ;Check for CR
  787.     BSR    HEXIN        ;Get PATTERN argument
  788.     BEQ    CMD_BAD        ;(PBRA) Check for 3 hex values
  789.     CMPI.L    #$000000FF,D0    ;Is it only one byte?
  790.     BHI.S    FIL20        ;Branch if not
  791. FIL10    MOVE.B    D0,(A0)+    ;Fill START through END with byte
  792.     CMPA.L    D1,A0        ; PATTERN
  793.     BLS.S    FIL10
  794.     BRA.S    FIL90        ;Exit
  795. FIL20
  796.     CMPI.L    #$0000FFFF,D0    ;Is it one word?
  797.     BHI.S    FIL40        ;Branch if not -- it's a long
  798. FIL30    MOVE.W    D0,(A0)+    ;Fill START through END with word
  799.     CMPA.L    D1,A0        ; PATTERN
  800.     BLS.S    FIL30
  801.     BRA.S    FIL90        ;Exit
  802. FIL40
  803. FIL50    MOVE.L    D0,(A0)+    ;Fill START through END with long
  804.     CMPA.L    D1,A0        ; PATTERN
  805.     BLS.S    FIL50
  806. FIL90    RTS
  807.  
  808. ;----------------------------------------------------------------------
  809. ;M command -- move a block of memory
  810. ; The block begins at the first argument, START, and ends at the second
  811. ; argument, END. The beginning of the block is moved to the address
  812. ; given by the third argument, TO.
  813. ;    MOVE (START, END, TO)
  814. ;
  815. ; Destroys registers D0, D1, A0, and A1.
  816. ;
  817. CMD_M    BSR    HEXIN        ;Get START argument
  818.     BEQ    CMD_BAD        ;(PBRA) Must have 3 hex values
  819.     MOVEA.L    D0,A0        ;Point A0 to START
  820.  
  821.     BSR    BACKUP        ;Check for CR
  822.     BSR    HEXIN        ;Get END argument
  823.     BEQ    CMD_BAD        ;(PBRA) Check for 3 hex values
  824.     CMP.L    A0,D0        ;If END < START then error
  825.     BLO    CMD_BAD        ;(PBRA)
  826.     SUB.L    A0,D0        ;LENGTH:= END - START + 1
  827.     ADDQ.L    #1,D0
  828.     MOVE.L    D0,D1        ;Save LENGTH in D1
  829.  
  830.     BSR    BACKUP        ;Check for CR
  831.     BSR    HEXIN        ;Get TO argument
  832.     BEQ    CMD_BAD        ;(PBRA) Check for 3 hex values
  833.     MOVEA.L    D0,A1
  834.  
  835.     MOVE.W    D1,D0        ;Get low 16 bits of LENGTH into D0 and
  836.     SWAP    D1        ; high 16 bits into D1
  837.  
  838.     CMPA.L    A0,A1        ;If TO > START (i.e: moving forward in
  839.     BEQ.S    MOVE90        ; memory) then don't branch, else
  840.     BLO.S    MOVE20        ; enter loop checking for LENGTH = 0
  841.                 ; (this is actually unnecessary)
  842.     ADDA.L    D0,A1        ;Move starting at the end of the block
  843.     ADDA.L    D0,A0        ;Add LENGTH to TO and START
  844.     BRA.S    MOVE40        ;Enter loop checking for LENGTH = 0
  845.  
  846. MOVE10    MOVE.B    (A0)+,(A1)+    ;Move block backward, pointers forward
  847. MOVE20    DBF    D0,MOVE10    ;Loop unitl D0 = -1
  848.     DBF    D1,MOVE10    ; and also D1 = -1
  849.     BRA.S    MOVE90        ;Exit
  850.  
  851. MOVE30    MOVE.B    -(A0),-(A1)    ;Move block forward, pointers backward
  852. MOVE40    DBF    D0,MOVE30    ;Loop unitl D0 = -1
  853.     DBF    D1,MOVE30    ; and also D1 = -1
  854. MOVE90    RTS
  855.  
  856. ;----------------------------------------------------------------------
  857. ;Z command -- set memory display frame size.
  858. ;
  859. CMD_Z    BSR    HEXIN        ;Get size argument
  860.     BEQ    CMD_BAD        ;(PBRA) We must have it, else error
  861.     MOVE.L    D0,SIZEDIS    ;Set new memory display size
  862.     RTS
  863.  
  864. ;======================================================================
  865. ;EXECUTE COMMANDS
  866. ;----------------------------------------------------------------------
  867. ;T command -- trace program
  868. ;
  869. CMD_T    BSET    #7,SAVSR+2    ;Set trace bit in status register
  870.  
  871.     BSR    HEXIN        ;Read entry point
  872.     BEQ.S    TR10
  873.     MOVE.L    D0,SAVPC    ;Update in save PC
  874. TR10
  875.     CLR.B    BPFLAG        ;Clear execute thru breakpoint flag
  876.     BRA    GO50        ;Enter common code
  877.  
  878. ;----------------------------------------------------------------------
  879. ;G command -- start or continue program
  880. ;
  881. CMD_G    BSR    HEXIN        ;Read entry point
  882.     BEQ.S    GO10
  883.     MOVE.L    D0,SAVPC    ;Update in save PC
  884. GO10
  885.     CLR.B    BPFLAG        ;Clear execute thru breakpoint flag
  886.     BCLR    #7,SAVSR+2    ;Clear trace mode
  887.  
  888. ;Handle breakpoints. This inserts the breakpoint TRAP #$F instructions.
  889. ; If we are about to execute a breakpoint, the TRAP is not inserted.
  890. ; Instead, the trace flag is set along with the BPFLAG which allows the
  891. ; instruction at the breakpoint to be executed. The trace flag causes
  892. ; control to be returned to the debugger which clears the trace flag
  893. ; and resumes execution of the user's program. The BPFLAG is used to
  894. ; distinguish this one-instruction trace from trace mode.
  895.  
  896.     LEA    BRKATBL.L,A1    ;Point to breakpoint table
  897.     LEA    BRKITBL.L,A2
  898.     MOVEQ    #7,D0        ;Set loop counter (7..0)
  899.  
  900. GO20    MOVEA.L    (A1)+,A0    ;Get breakpoint address
  901.     MOVE.W    (A0),(A2)+    ;Save instruction in table
  902.     CMPA.L    SAVPC,A0    ;Are we about to execute a breakpoint?
  903.     BNE.S    GO30        ;Branch if not
  904.     BSET    #7,SAVSR+2    ;Set trace bit
  905.     ST    BPFLAG        ;Set flag to continue thru breakpoint
  906.     BRA.S    GO40
  907.  
  908. GO30    MOVE.W    #$4E4F,(A0)    ;Replace instruction with TRAP #15
  909. GO40    DBF    D0,GO20        ;Loop for all breakpoints
  910.  
  911. ;Enter from "continue past breakpoint" code
  912. GO50    MOVEA.L    SAVUSP,A0    ;Restore user stack pointer
  913.     MOVE    A0,USP
  914.     MOVEA.L    SAVA+28,SP    ;Restore supervisor stack pointer
  915.     MOVE.L    SAVPC,-(SP)    ;Stack saved PC
  916.     MOVE.W    SAVSR+2,-(SP)    ;Stack saved SR
  917.     MOVEM.L    SAVD,D0-D7/A0-A6  ;Restore user's registers
  918.     ST    GOFLAG        ;Indicate that the user's prog is going
  919.     RTE            ;Restore PC & SR and resume execution
  920.  
  921. ;======================================================================
  922. ;EXCEPTION HANDLERS
  923. ;----------------------------------------------------------------------
  924. ;Jump table for exception handlers.
  925. ; The exception vectors point to the various entries in this table.
  926. ; There is an entry for each type of exception. BSR.S pushes a return
  927. ; address which is used to determine which type of exception occurred.
  928. ;
  929. BE_EXCP    BSR.S    EXCP10        ;2 Bus Error
  930. AE_EXCP    BSR.S    EXCP10        ;3 Address Error
  931. II_EXCP    BSR.S    EXCP20        ;4 Illegal Instruction
  932. ZD_EXCP    BSR.S    EXCP20        ;5 Zero Divide
  933. CI_EXCP    BSR.S    EXCP20        ;6 CHK Instruction
  934. TV_EXCP    BSR.S    EXCP20        ;7 TRAPV Instruction
  935. PV_EXCP    BSR.S    EXCP20        ;8 Privilege Violation
  936. TR_EXCP    BSR.S    EXCP20        ;9 Trace
  937. LA_EXCP    BSR.S    EXCP20        ;10 Line 1010 Emulator
  938. LF_EXCP    BSR.S    EXCP20        ;11 Line 1111 Emulator
  939. SI_EXCP    BSR.S    EXCP20        ;12 Spurious Interrupt
  940. IN_EXCP    BSR.S    EXCP20        ;13 Unassigned Interrupt
  941. TI_EXCP    BSR.S    EXCP20        ;14 Unassigned TRAP Instruction
  942. BP_EXCP    BSR.S    EXCP20        ;15 TRAP #$F (Breakpoint)
  943. UA_EXCP    BSR.S    EXCP20        ;16 Unassigned exception
  944.  
  945. ;----------------------------------------------------------------------
  946. ;Exception handler for bus errors and address errors (group 0).
  947. ; After D0 and A0 are pushed, the supervisor stack looks like this:
  948. ;
  949. ;    SSP -->     0  D0 Hi
  950. ;         2  D0 Lo
  951. ;         4  A0 Hi
  952. ;         6  A0 Lo
  953. ;         8  Return Address Hi
  954. ;        10  Return Address Lo
  955. ;        12  Function Codes, etc.
  956. ;        14  Access Address Hi
  957. ;        16  Access Address Lo
  958. ;        18  Instruction Register
  959. ;        20  Status Register
  960. ;        22  PC Hi
  961. ;        24  PC Lo
  962. ;
  963. ;
  964. EXCP10    MOVEM.L    D0/A0,-(SP)    ;Save registers
  965.  
  966.     MOVE.L    14(SP),ACCESS    ;Save access address and function
  967.     MOVE.W    12(SP),FUNCODE    ; codes to be displayed later
  968.  
  969.     MOVEA.L    22(SP),A0    ;Get PC
  970.     MOVE.W    18(SP),D0    ;Get instruction register
  971.  
  972.     CMP.W    -(A0),D0    ;Since the PC value varies a few words,
  973.     BEQ.S    EXCP12        ; look for the instruction in the code
  974.     CMP.W    -(A0),D0    ; that matches the instruction register
  975.     BEQ.S    EXCP12        ; and set PC to point to it.
  976.     CMP.W    -(A0),D0
  977.     BEQ.S    EXCP12
  978.     CMP.W    -(A0),D0
  979.     BEQ.S    EXCP12
  980.     SUBQ    #2,A0
  981. EXCP12
  982.     MOVE.L    A0,22(SP)    ;Restore corrected PC
  983.  
  984.     MOVEM.L    (SP)+,D0/A0    ;Restore registers
  985.     MOVE.L    (SP),8(SP)    ;Move return address ahead and adjust
  986.     ADDQ    #8,SP        ; stack to look like a group 1 or 2
  987.                 ; execption, and fall into EXCP20
  988.  
  989. ;----------------------------------------------------------------------
  990. ;Exception handler for group 1 and 2 exceptions.
  991. ; Upon entering, the supervisor stack looks like this:
  992. ;
  993. ;    SSP -->  0  Return Address Hi (exception type)
  994. ;         2  Return Address Lo (exception type)
  995. ;         4  Status Register
  996. ;         6  PC Hi
  997. ;         8  PC Lo
  998. ;
  999. ;
  1000. EXCP20    TST.B    GOFLAG        ;Don't save registers unless we were
  1001.     BEQ.S    EXCP25        ; running the user's program
  1002.     MOVEM.L    D0-D7/A0-A7,SAVD  ;Save all user registers
  1003. EXCP25
  1004.     MOVEA.L    (SP)+,A1    ;Get address of routine which called
  1005.     SUBQ.L    #2,A1        ; this exception handler and save in A1
  1006.  
  1007.     MOVE.W    (SP)+,D0    ;Get SR and PC from the stack
  1008.     MOVEA.L    (SP)+,A0
  1009.  
  1010.     TST.B    GOFLAG        ;Don't save registers unless we were
  1011.     BEQ.S    EXCP28        ; running the user's program
  1012.     MOVE.W    D0,SAVSR+2    ;Save SR
  1013.     MOVE.L    A0,SAVPC    ;Save PC
  1014.     MOVE    USP,A0        ;Get user stack pointer
  1015.     MOVE.L    A0,SAVUSP    ;Save USP
  1016.     MOVE.L    SP,SAVA+28    ;Save SP
  1017.     CLR.B    GOFLAG        ;We're no longer running the user prog
  1018.     BRA.S    EXCP29
  1019.  
  1020. EXCP28    BSR    CRLF        ;New line if internal exception
  1021. EXCP29
  1022.  
  1023. ;Restore all breakpoints with their original instructions
  1024.     LEA    BRKATBL.L,A0    ;Point A0 to breakpoint table
  1025.     LEA    BRKITBL.L,A2
  1026.     MOVEQ    #7,D0        ;Loop for 8 breakpoints (0..7)
  1027. EXCP30    MOVEA.L    (A0)+,A3    ;Get the address of the breakpoint
  1028.     MOVE.W    (A2)+,(A3)    ;Restore the original instruction
  1029.     DBF    D0,EXCP30    ;Loop for all breakpoints
  1030.  
  1031.     LEA    TR_EXCP.L,A0    ;Is this a trace exception?
  1032.     CMPA.L    A0,A1
  1033.     BNE.S    EXCP50        ;Branch if not
  1034.  
  1035. ;Check for continue through breakpoint (i.e. a one-instruction trace)?
  1036.     TST.B    BPFLAG        ;Is this a continue through breakpoint?
  1037.     BEQ.S    EXCP40        ;Branch if not
  1038.  
  1039.     CLR.B    BPFLAG
  1040.     BCLR    #7,SAVSR+2    ;Turn trace bit off
  1041.  
  1042. ;Insert the missing breakpoint. (We must insert them all, because we
  1043. ; don't know how many words were in the last instruction.)
  1044.     LEA    BRKATBL.L,A1    ;Point to breakpoint table
  1045.     LEA    BRKITBL.L,A2
  1046.     MOVEQ    #7,D0        ;Set loop counter (7..0)
  1047. EXCP35    MOVEA.L    (A1)+,A0    ;Get breakpoint address
  1048.     MOVE.W    (A0),(A2)+    ;Save instruction in table
  1049.     MOVE.W    #$4E4F,(A0)    ;Replace instruction with TRAP #15
  1050.     DBF    D0,EXCP35    ;Loop for all breakpoints
  1051.     BRA    GO50        ;Resume execution of user's program
  1052.  
  1053. ;Handle normal trace exception
  1054. EXCP40    BSR    TEXTABO        ;Display trace message
  1055.     BSR    CMD_DR        ;Display registers
  1056.  
  1057.     LEA    CMDTBL.L,A0    ;Point to command table
  1058.     PEA    GETCMD.L    ;Push ret adr of com loop
  1059.     BSR    TEXT        ;Display prompt for special trace mode
  1060.     DC.B    'T', '>' + $80
  1061.     BSR    GETCH        ;Get next chr
  1062.     CMP.B    #CR,D0        ;Carriage return?
  1063.     BNE    EXECUTE        ;(PBNE) No, search for command
  1064.     BSET    #7,SAVSR+2    ;Set trace bit in status register
  1065.     BRA    TR10
  1066.  
  1067. ;Check if this is a breakpoint
  1068. EXCP50    LEA    BP_EXCP.L,A0    ;Is it a TRAP #$F?
  1069.     CMPA.L    A0,A1
  1070.     BNE.S    EXCP60        ;Branch if not
  1071.  
  1072. ;Is the TRAP #$F actually one of our breakpoints?
  1073. ;If an entry in BRKATBL = SAVPC -2 then it is.
  1074.     MOVEA.L    SAVPC,A0    ;Get PC and adjust it to point to the
  1075.     SUBQ.L    #2,A0        ; TRAP #$F instruction
  1076.     LEA    BRKATBL.L,A2    ;Point to breakpoint address table
  1077.     MOVEQ    #7,D1        ;Loop for 8 breakpoints
  1078. EXCP53    CMPA.L    (A2)+,A0    ;Does this entry match the address?
  1079.     DBEQ    D1,EXCP53    ;Decrement and branch if not
  1080.     BNE.S    EXCP59        ;Branch if not
  1081.  
  1082.     MOVE.L    A0,SAVPC    ;Fix PC to point to address of TRAP
  1083.     BSR    TEXTABO        ;Display breakpoint message
  1084.     BSR    CMD_DR        ;Display registers
  1085.     BRA    GETCMD        ;Enter main loop of debugger
  1086.  
  1087. EXCP59    LEA    TI_EXCP.L,A1    ;Change "BREAKPOINT" to "TRAP"
  1088.  
  1089. ;Check for interrupt exception
  1090. EXCP60    LEA    IN_EXCP.L,A0    ;Interrupt exception?
  1091.     CMPA.L    A0,A1
  1092.     BNE.S    EXCP70        ;Branch if not
  1093.  
  1094.     BSR    TEXT
  1095.     ASCII    'INTERRUPT AT LEVEL'
  1096.     DC.B    BEL, SPACE + $80
  1097.     MOVE    SR,D0        ;Get interrupt level from SR
  1098.     LSR.W    #8,D0
  1099.     BSR    HEX1OUT
  1100.     BSR    TEXTABO        ;Display message
  1101.     BRA    GETCMD        ;Enter main loop of debugger
  1102.  
  1103. ;Check for TRAP instructions
  1104. EXCP70    LEA    TI_EXCP.L,A0    ;Trap instruction?
  1105.     CMPA.L    A0,A1
  1106.     BNE.S    EXCP80        ;Branch if not
  1107.  
  1108.     BSR    TEXT
  1109.     ASCII    'TRAP #'
  1110.     DC.B    BEL, '$' + $80
  1111.     MOVEA.L    SAVPC,A0    ;Get trap level from instruction
  1112.     MOVE.W    -(A0),D0
  1113.     BSR    HEX1OUT
  1114.  
  1115.     BSR    TEXT
  1116.     ASCII    ' INSTRUCTIO'
  1117.     DC.B    'N' + $80
  1118.     BSR    TEXTABO        ;Display message
  1119.     BRA    GETCMD        ;Enter main loop of debugger
  1120.  
  1121. ;Check for bus and address error exceptions
  1122. EXCP80    LEA    BE_EXCP.L,A0    ;Bus error?
  1123.     CMPA.L    A0,A1
  1124.     BEQ.S    EXCP85        ;Branch if it is
  1125.     LEA    AE_EXCP.L,A0    ;Address error?
  1126.     CMPA.L    A0,A1
  1127.     BNE.S    EXCP90        ;Branch if not
  1128. EXCP85
  1129.     BSR.S    BEEP
  1130.     BSR.S    TEXTABO
  1131.     BSR    TEXT
  1132.     ASCII    'ACCESS ADDRESS:'
  1133.     DC.B    SPACE + $80
  1134.     MOVE.L    ACCESS,D0
  1135.     BSR    HEX8OUT
  1136.  
  1137.     BSR    TEXT
  1138.     DC.B    CR
  1139.     ASCII    'FUNCTION CODE:'
  1140.     DC.B    SPACE + $80
  1141.     MOVE.W    FUNCODE,D0
  1142.     BSR    HEX2OUT
  1143.     BSR    CRLF
  1144.     BRA    GETCMD        ;Enter main loop of debugger
  1145.  
  1146. ;Display message for remaining exceptions
  1147. EXCP90    BSR.S    BEEP
  1148.     BSR    TEXTABO        ;Display exception error message
  1149.     BRA    GETCMD        ;Enter main loop of debugger
  1150.  
  1151. ;----------------------------------------------------------------------
  1152. ;Make beep noise
  1153. ;
  1154. BEEP    MOVEQ    #BEL,D0        ;Beep
  1155.     BRA    CHOUT        ;(PBRA)
  1156.  
  1157. ;----------------------------------------------------------------------
  1158. ;Display an exception message.
  1159. ; Inputs:
  1160. ;    A1 = Address of exception handler
  1161. ; Destroys D0, A0, A1, and A2
  1162. ;
  1163. TEXTABO    LEA    BE_EXCP.L,A0    ;Get base address of jump table
  1164.     SUBA.L    A0,A1        ;Get word offset for current exception
  1165.     MOVE.L    A1,D0
  1166.     LEA    ABOTBL.L,A2    ;Get pointer to abort table base
  1167.     MOVEA.W    0(A2,D0.L),A0    ;Get offset to base of message
  1168.     ADDA.L    A2,A0        ;Add base to point to string
  1169.     BSR    TEXTA0        ;(PBRA) Display message and return
  1170.  
  1171.     BSR    TEXT
  1172.     ASCII    '   PC: '
  1173.     DC.B    SPACE + $80
  1174.     MOVEA.L    SAVPC,A0    ;Get current PC
  1175.     MOVE.L    A0,D0        ;Display it
  1176.     BSR    HEX8OUT
  1177.     BSR    TEXT
  1178.     ASCII    ': '
  1179.     DC.B    SPACE + $80
  1180.  
  1181.     MOVE.W    (A0),D0        ;Display instruction at PC
  1182.     BSR    HEX4OUT        ; (this is the next instruction to be
  1183.     BSR    CRLF        ; executed)
  1184.  
  1185.     RTS
  1186.  
  1187. ;Table of pointers to exception messages.
  1188. ABOTBL    DC.W    AB2-ABOTBL
  1189.     DC.W    AB3-ABOTBL
  1190.     DC.W    AB4-ABOTBL
  1191.     DC.W    AB5-ABOTBL
  1192.     DC.W    AB6-ABOTBL
  1193.     DC.W    AB7-ABOTBL
  1194.     DC.W    AB8-ABOTBL
  1195.     DC.W    AB9-ABOTBL
  1196.     DC.W    AB10-ABOTBL
  1197.     DC.W    AB11-ABOTBL
  1198.     DC.W    AB12-ABOTBL
  1199.     DC.W    AB13-ABOTBL
  1200.     DC.W    AB14-ABOTBL
  1201.     DC.W    AB15-ABOTBL
  1202.     DC.W    AB16-ABOTBL
  1203.  
  1204. AB2    ASCII    'BUS ERROR'
  1205.     DC.B    SPACE + $80
  1206. AB3    ASCII    'ADDRESS ERROR'
  1207.     DC.B    SPACE + $80
  1208. AB4    ASCII    'ILLEGAL INSTRUCTION'
  1209.     DC.B    SPACE + $80
  1210. AB5    ASCII    'ZERO DIVIDE'
  1211.     DC.B    SPACE + $80
  1212. AB6    ASCII    'CHK INSTRUCTION'
  1213.     DC.B    SPACE + $80
  1214. AB7    ASCII    'TRAPV INSTRUCTION'
  1215.     DC.B    SPACE + $80
  1216. AB8    ASCII    'PRIVILEGE VIOLATION'
  1217.     DC.B    SPACE + $80
  1218. AB9    ASCII    'TRACE'
  1219.     DC.B    SPACE + $80
  1220. AB10    ASCII    'LINE 1010 EMULATOR'
  1221.     DC.B    SPACE + $80
  1222. AB11    ASCII    'LINE 1111 EMULATOR'
  1223.     DC.B    SPACE + $80
  1224. AB12    ASCII    'SPURIOUS INTERRUPT'
  1225.     DC.B    SPACE + $80
  1226. AB13    DC.B    SPACE + $80
  1227. AB14    DC.B    SPACE + $80
  1228. AB15    ASCII    'BREAKPOINT'
  1229.     DC.B    SPACE + $80
  1230. AB16    ASCII    'UNASSIGNED EXCEPTION'
  1231.     DC.B    SPACE + $80
  1232.  
  1233. ;======================================================================
  1234. ;SUBROUTINES
  1235. ;----------------------------------------------------------------------
  1236. ;Search the command table pointed to by A0 for the character in D0.
  1237. ; This calls the routine which corresponds to the character in D0.
  1238. ; The calling technique is a bit tricky. When the called command routine
  1239. ; returns, it returns to the location where this search routine was
  1240. ; called.
  1241. ;
  1242. EXECUTE    MOVE.L    D1,-(SP)    ;Save D1 (MOVEM won't work here)
  1243.     MOVE.L    A0,-(SP)    ; and A0
  1244.     BRA.S    SRCH20        ;Enter search loop
  1245.  
  1246. SRCH10    CMP.B    D1,D0        ;Does entry match character in D0?
  1247.     BEQ.S    SRCH30        ;Branch if it does -- found
  1248.     ADDQ.W    #2,A0        ;Skip rest of the entry
  1249. SRCH20    MOVE.W    (A0)+,D1    ;Get character from table
  1250.     BNE.S    SRCH10        ;Loop if not end of table
  1251.  
  1252. SRCH30    MOVEA.W (A0)+,A0
  1253.     ADDA.L    (SP),A0        ;Add table base addr, the original A0
  1254.  
  1255.     MOVE.L    4(SP),D1    ;Restore D1
  1256.     MOVE.L    A0,4(SP)    ;Put routine address on stack
  1257.     MOVEA.L    (SP)+,A0    ;Restore A0
  1258.     RTS            ;Call routine
  1259.  
  1260. ;======================================================================
  1261. ;INPUT ROUTINES
  1262. ;----------------------------------------------------------------------
  1263. ;Get character from keyboard buffer, convert it to upper case, and
  1264. ; return it in D0.
  1265. ;
  1266. GETCH    BSR    CHIN
  1267.  
  1268.     CMPI.B    #'a',D0        ;Shift character in D0 to uppercase
  1269.     BLO.S    UPC90
  1270.     CMPI.B    #'z',D0
  1271.     BHI.S    UPC90
  1272.     ANDI.B    #$DF,D0        ;Convert to uppercase
  1273.  
  1274. UPC90    RTS
  1275.  
  1276. ;-----------------------------------------------------------------------
  1277. ;This routine inputs hex ASCII digits from the terminal and converts
  1278. ; them to a 32-bit binary value which is returned in D0. 
  1279. ;
  1280. ; If a hex value is successfully read, the Z flag is returned cleared
  1281. ; (NE status). This routine skips any initial non-hex characters, but
  1282. ; always returns when it finds a carriage return (CR). If a CR is read
  1283. ; before finding any hex digit, this returns with the Z flag set (EQ
  1284. ; status) and D0 = 0. A Hex value is normally terminated by a non-hex
  1285. ; character, such as a space or CR.
  1286. ;
  1287. ; If the hex value is more than 8 digits long, only the first 8 digits
  1288. ; will be read in, thus allowing additional calls to handle all of the
  1289. ; digits.
  1290. ;
  1291. ; The hex characters A - F may be lowercase (a - f).
  1292. ;
  1293. ; Register Usage:
  1294. ;    D0 = Digit
  1295. ;    D1 = Accumulated value
  1296. ;    D2 = Digit counter
  1297. ;
  1298. ;
  1299. HEXIN    MOVEM.L    D1-D2,-(SP)    ;Save registers
  1300.     MOVEQ    #0,D1        ;Clear result register
  1301.     MOVEQ    #7,D2        ;Init digit counter (7 down through 0)
  1302.  
  1303. HEXI00    BSR    CHIN        ;Get character
  1304.     CMPI.B    #CR,D0        ;Is it a carriage return?
  1305.     BEQ.S    HEXI90        ;Branch if yes -- exit
  1306.     CMPI.B    #UP,D0        ;Is it an up arrow?
  1307.     BEQ.S    HEXI90        ;Branch if yes -- exit
  1308.     CMPI.B    #DOWN,D0    ;Is it a down arrow?
  1309.     BEQ.S    HEXI90        ;Branch if yes -- exit
  1310.  
  1311.     CMPI.B    #'0',D0        ;Is character in range 0 thru 9?
  1312.     BLO.S    HEXI40        ;Branch if not
  1313.     CMPI.B    #'9',D0
  1314.     BHI.S    HEXI20        ;(May be A-F)
  1315.     SUBI.B    #'0',D0        ;Convert ASCII to binary value
  1316.     BRA.S    HEXI30        ;Go combine with other digits
  1317.  
  1318. HEXI20    ANDI.B    #$DF,D0        ;Force to uppercase
  1319.     CMPI.B    #'A',D0        ;Is character in range A through F?
  1320.     BLO.S    HEXI40        ;Branch if not -- it's not hex
  1321.     CMPI.B    #'F',D0
  1322.     BHI.S    HEXI40
  1323.     SUBI.B    #'A'-10,D0    ;Convert ASCII to binary value
  1324.  
  1325. HEXI30    ASL.L    #4,D1        ;Multiply current value by 16
  1326.     ADD.B    D0,D1        ;Add new digit
  1327.     DBF    D2,HEXI00    ;Exit if we have 8 digits
  1328.  
  1329. HEXI40    CMPI.B    #7,D2        ;Did we find a hex digit?
  1330.     BEQ.S    HEXI00        ;Branch if not -- keep trying
  1331.  
  1332. HEXI90    MOVE.L    D1,D0        ;Return the hex value in D0
  1333.     CMPI.B    #7,D2        ;Set Z flag if no digits were read
  1334.     MOVEM.L    (SP)+,D1-D2    ;Restore registers
  1335.     RTS
  1336.  
  1337. ;======================================================================
  1338. ;OUTPUT ROUTINES
  1339. ;----------------------------------------------------------------------
  1340. ;Output a space character.
  1341. ;
  1342. SPOUT    MOVE.L    D0,-(SP)    ;Save D0
  1343.  
  1344.     MOVEQ    #SPACE,D0    ;Load a space character
  1345.     BSR    CHOUT        ;Output it
  1346.  
  1347.     MOVE.L    (SP)+,D0    ;Restore D0
  1348.     RTS
  1349.  
  1350. ;----------------------------------------------------------------------
  1351. ;Output a text string. The string immediately follows the call to this
  1352. ; routine. I.e:
  1353. ;    BSR    TEXT        ;Call to this subroutine
  1354. ;    ASCII    'STRIN'        ;String to output
  1355. ;    DC.B    'G' + $80    ;Last character must have MSB set
  1356. ;
  1357. TEXT    MOVEM.L    A0/A1,-(SP)    ;Save registers
  1358.  
  1359.     MOVEA.L    8(SP),A0    ;Get the "return address" which points
  1360.     MOVEA.L    A0,A1        ; to the text string, and get a copy
  1361. TXT10    TST.B    (A1)+        ;Scan for the end-of-string (MSB set)
  1362.     BPL.S    TXT10        ;Loop unitl the last character is found
  1363.  
  1364.     ADDQ.L    #1,A1        ;Set up the real return address
  1365.     MOVE.L    A1,8(SP)    ;Make sure it is on the word boundary
  1366.     ANDI.B    #$FE,11(SP)    ; following the string
  1367.  
  1368.     BSR.S    TEXTA0        ;Output the string pointed to by A0
  1369.  
  1370.     MOVEM.L    (SP)+,A0/A1    ;Restore registers
  1371.     RTS
  1372.  
  1373. ;----------------------------------------------------------------------
  1374. ;Output a text string pointed to by A0.
  1375. ; The string is terminated with a character having its MSB set.
  1376. ;
  1377. TEXTA0    MOVEM.L    D0/A0,-(SP)    ;Save registers
  1378.  
  1379.     BRA.S    TXTA20        ;Enter loop
  1380.  
  1381. TXTA10    BSR    CHOUT        ;Output D0
  1382. TXTA20    MOVE.B    (A0)+,D0    ;Get char from string
  1383.     BPL.S    TXTA10        ;Loop unitl the last character
  1384.  
  1385.     ANDI.B    #$7F,D0        ;Clear MSB
  1386.     BSR    CHOUT        ;Output D0
  1387.  
  1388.     MOVEM.L    (SP)+,D0/A0    ;Restore registers
  1389.     RTS
  1390.  
  1391. ;----------------------------------------------------------------------
  1392. ;Output D0 in ASCII hex (8 digits).
  1393. ;
  1394. HEX8OUT    SWAP    D0        ;Get high word
  1395.     BSR.S    HEX4OUT        ;Output it
  1396.     SWAP    D0        ;(PFALL) get low word back
  1397.  
  1398. ;----------------------------------------------------------------------
  1399. ;Output D0 in ASCII hex (4 digits)
  1400. ;
  1401. HEX4OUT    ROR.W    #8,D0        ;Move high byte down (and save low byte)
  1402.     BSR.S    HEX2OUT        ;Output it
  1403.     ROR.W    #8,D0        ;(PFALL) get low byte
  1404.  
  1405. ;----------------------------------------------------------------------
  1406. ;Output D0 in ASCII hex (2 digits)
  1407. ;
  1408. HEX2OUT    ROR.B    #4,D0        ;Move high nybble down (save low nybble)
  1409.     BSR.S    HEX1OUT        ;Output it
  1410.     ROR.B    #4,D0        ;(PFALL) get low nybble
  1411.  
  1412. ;----------------------------------------------------------------------
  1413. ;Output D0 in ASCII hex (1 digit)
  1414. ;
  1415. HEX1OUT    MOVE.B    D0,-(SP)    ;Save D0
  1416.  
  1417.     ANDI.B    #$0F,D0        ;Work with low nybble only
  1418.     CMPI.B    #10,D0
  1419.     BLO.S    H1O10
  1420.     ADDQ.B    #7,D0
  1421. H1O10    ADDI.B    #'0',D0        ;Convert to ASCII
  1422.     BSR    CHOUT        ;Output digit
  1423.  
  1424.     MOVE.B    (SP)+,D0    ;Restore D0
  1425.     RTS
  1426.  
  1427. ;======================================================================
  1428. ;CONSOLE HANDLER ROUTINES
  1429. ;
  1430. OPEN    BSR.S    OPENI
  1431.     BRA.S    OPENO
  1432.  
  1433. ;
  1434. CRLF    MOVE.L    D0,-(SP)
  1435.     MOVEQ    #CR,D0
  1436.     BSR.S    CHOUT
  1437.     MOVE.L    (SP)+,D0
  1438.     RTS
  1439.  
  1440. ;
  1441. BACKUP    ST    BKFLAG
  1442.     RTS
  1443.  
  1444. ;
  1445. OPENI    CLR.B    BKFLAG
  1446.     MOVEM.L    A6,-(SP)
  1447.     CLR.B    DEVICE
  1448.     MOVEA.W    #0,A6
  1449.     JSR    VDEVHAN
  1450.     MOVEA.L    (SP)+,A6
  1451.     RTS
  1452.  
  1453. ;
  1454. OPENO    MOVEM.L    A6,-(SP)
  1455.     CLR.B    DEVICE
  1456.     MOVEA.W    #4,A6
  1457.     JSR    VDEVHAN
  1458.     MOVEA.L    (SP)+,A6
  1459.     RTS
  1460.  
  1461. ;
  1462. CHIN    TST.B    BKFLAG
  1463.     BEQ.S    CHIN20
  1464.     CLR.B    BKFLAG
  1465.     MOVEQ    #0,D0
  1466.     MOVE.B    BKCHAR,D0
  1467.     BRA.S    CHIN90
  1468. CHIN20
  1469.     MOVEM.L    A6,-(SP)
  1470.     CLR.B    DEVICE
  1471.     MOVEA.W    #8,A6
  1472.     JSR    VDEVHAN
  1473.     MOVE.B    D0,BKCHAR
  1474.     MOVEA.L    (SP)+,A6
  1475. CHIN90    RTS
  1476.  
  1477. ;
  1478. CHOUT    MOVE.L    A6,-(SP)
  1479.     CLR.B    DEVICE
  1480.     MOVEA.W    #12,A6
  1481.     JSR    VDEVHAN
  1482.     MOVEA.L    (SP)+,A6
  1483.     RTS
  1484.  
  1485.  
  1486. ;----------------------------------------------------------------------
  1487. ;APEX SYSPAG PARAMETERS
  1488.  
  1489. END    EQU    @
  1490.  
  1491.     ORG    USRMEM
  1492.     DC.L    BASE
  1493.  
  1494.     ORG    PROSIZ
  1495.     DC.L    [END-BASE+255]/$100
  1496.  
  1497.     END
  1498. ----------
  1499. ;APEX SYSPAG PARAMETERS
  1500.  
  1501. END    EQU    @
  1502.  
  1503.     ORG    USRMEM
  1504.     DC.L    BA